//$URL: http://dasding.googlecode.com/svn/branches/testzeugs/api/src/main/java/de/piratech/dasding/api/rest/EventIdResource.java $
//$Id: EventIdResource.java 107 2013-03-29 14:27:07Z krieg.maria@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.api.rest;

import java.util.Date;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import de.micromata.opengis.kml.v_2_2_0.Document;
import de.piratech.dasding.data.Event;
import de.piratech.dasding.data.database.DatabaseException;

/**
 * Resource used api calls for a specific event
 * 
 * @author deveth0
 */
@Path("/events/id/{id}")
@Component
public class EventIdResource extends AbstractResource {

  private static final Logger LOG = LoggerFactory
      .getLogger(EventIdResource.class);

  /**
   * Returns a single event by id
   * 
   * http://localhost:8080/dasding.api/api/events/id/1
   * 
   * GET
   * 
   * @param id
   * @return
   */
  @GET
  @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
  public Event getEvent(@PathParam("id") String id) {
    LOG.debug("getEvent({})", id);
    try {
      return getEventData().getEvent(id);
    } catch (DatabaseException dbe) {
      throw getBadRequestException(dbe);
    }
  }

  /**
   * Returns the event as kml
   * 
   * http://localhost:8080/dasding.api/api/events/id/1.kml
   * 
   * GET
   * 
   * @param id
   * @return
   */
  @GET
  @Produces("application/vnd.google-earth.kml+xml")
  public Document getEventAsKML(@PathParam("id") String id) {
    LOG.debug("getEventAsKML({})", id);
    try {
      return getEventData().getEvent(id).getKML();
    } catch (DatabaseException dbe) {
      throw getBadRequestException(dbe);
    }
  }

  /**
   * Updates the Event with the given id
   * 
   * http://localhost:8080/dasding.api/api/events/id/1
   * 
   * PUT
   * 
   * @param id
   * @param name
   * @param description
   * @param time
   * 
   * @return
   */
  @PUT
  @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
  public Response updateEvent(@PathParam("id") String id,
      @QueryParam("name") String name,
      @QueryParam("description") String description,
      @QueryParam("time") Long time) {
    LOG.debug("updateEvent(id={}, name={}, description={}, time={})", id, name,
        description, time);
    try {
      Event event = getEventData().getEvent(id);
      if (event != null) {
        if (StringUtils.isNotBlank(name)) {
          event.setName(name);
        }
        if (StringUtils.isNotBlank(description)) {
          event.setDescription(description);
        }
        if (time != null) {
          event.setTime(new Date(time));
        }
        if (!event.isValid()) {
          return getBadRequestResponse("Event is not valid");
        }
      } else {
        return getBadRequestResponse("Event not found");
      }
      return getNoContentResponse();
    } catch (DatabaseException dbe) {
      return getBadRequestResponse(dbe);
    }
  }

  /**
   * Sets the given event to status "Canceled"
   * 
   * http://localhost:8080/dasding.api/api/events/id/1
   * 
   * DELETE
   * 
   * @param id
   */
  @DELETE
  @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
  public Response deleteEvent(@PathParam("id") String id) {
    LOG.debug("deleteEvent({})", id);
    Event event = getEvent(id);
    if (event != null) {
      event.setStatus(Event.EVENTSTATUS.CANCELED);
    } else {
      return getBadRequestResponse("Event not found");
    }
    return getNoContentResponse();
  }

  @Override
  protected Logger getLogger() {
    return LOG;
  }
}
